namespace SmartSoft.SystemForms
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Properties;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormGroupEditor : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnSave;
        private ChkControl chkGrpType;
        private IContainer components;
        private bool ib_NewChecked;
        private string is_AddTitleText = "Thêm nhóm người sử dụng";
        private string is_EditTitleText = "Sửa nhóm người sử dụng";
        private LabelControl lblGrpCode;
        private LabelControl lblGrpName;
        private LabelControl lblGrpType;
        private DataRow oCurrentRow;
        private FormGroupLister oLocalFormLister;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStrip Toolbars;
        private TextControl txtGrpCode;
        private TextControl txtGrpName;

        public FormGroupEditor()
        {
            this.InitializeComponent();
            this.chkGrpType.Enabled = AppInstance.UserType == -1;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                this.NewChecked = true;
                this.OnReset();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                base.Close();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private string GetTitleMessage()
        {
            if (!this.NewChecked)
            {
                return this.EditTitleText;
            }
            return this.AddTitleText;
        }

        private void InitializeComponent()
        {
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.lblGrpName = new SmartSoft.Controls.LabelControl();
            this.lblGrpCode = new SmartSoft.Controls.LabelControl();
            this.txtGrpName = new SmartSoft.Controls.TextControl();
            this.txtGrpCode = new SmartSoft.Controls.TextControl();
            this.chkGrpType = new SmartSoft.Controls.ChkControl();
            this.lblGrpType = new SmartSoft.Controls.LabelControl();
            this.Toolbars.SuspendLayout();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnAdd,
            this.tbrSep01,
            this.btnSave,
            this.tbrSep02,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(525, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::SmartSoft.Properties.Resources.BALANCE1;
            this.btnAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm mới (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSave
            // 
            this.btnSave.Image = global::SmartSoft.Properties.Resources.SAVE1;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(47, 22);
            this.btnSave.Text = "Lưu";
            this.btnSave.ToolTipText = "Ghi lại và Thoát (Ctrl+Enter)";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClose
            // 
            this.btnClose.Image = global::SmartSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // lblGrpName
            // 
            this.lblGrpName.AutoSize = true;
            this.lblGrpName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblGrpName.Location = new System.Drawing.Point(12, 63);
            this.lblGrpName.Name = "lblGrpName";
            this.lblGrpName.Size = new System.Drawing.Size(54, 14);
            this.lblGrpName.TabIndex = 7;
            this.lblGrpName.Text = "Tên nhóm";
            // 
            // lblGrpCode
            // 
            this.lblGrpCode.AutoSize = true;
            this.lblGrpCode.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblGrpCode.Location = new System.Drawing.Point(12, 41);
            this.lblGrpCode.Name = "lblGrpCode";
            this.lblGrpCode.Size = new System.Drawing.Size(50, 14);
            this.lblGrpCode.TabIndex = 8;
            this.lblGrpCode.Text = "Mã nhóm";
            // 
            // txtGrpName
            // 
            this.txtGrpName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtGrpName.CheckTextSearched = false;
            this.txtGrpName.DisabledForeColor = System.Drawing.Color.Empty;
            this.txtGrpName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtGrpName.Location = new System.Drawing.Point(123, 60);
            this.txtGrpName.MaxLength = 128;
            this.txtGrpName.Name = "txtGrpName";
            this.txtGrpName.OldValue = null;
            this.txtGrpName.Size = new System.Drawing.Size(385, 20);
            this.txtGrpName.TabIndex = 1;
            this.txtGrpName.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemDescription;
            this.txtGrpName.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.All;            
            // 
            // txtGrpCode
            // 
            this.txtGrpCode.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.txtGrpCode.CheckTextSearched = false;
            this.txtGrpCode.DisabledForeColor = System.Drawing.Color.Empty;
            this.txtGrpCode.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtGrpCode.Location = new System.Drawing.Point(123, 38);
            this.txtGrpCode.MaxLength = 32;
            this.txtGrpCode.Name = "txtGrpCode";
            this.txtGrpCode.OldValue = null;
            this.txtGrpCode.Size = new System.Drawing.Size(385, 20);
            this.txtGrpCode.TabIndex = 0;
            this.txtGrpCode.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemCode;
            this.txtGrpCode.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.All;            
            // 
            // chkGrpType
            // 
            this.chkGrpType.AutoSize = true;
            this.chkGrpType.Font = new System.Drawing.Font("Arial", 8.25F);
            this.chkGrpType.Location = new System.Drawing.Point(123, 85);
            this.chkGrpType.Name = "chkGrpType";
            this.chkGrpType.Size = new System.Drawing.Size(15, 14);
            this.chkGrpType.TabIndex = 2;
            // 
            // lblGrpType
            // 
            this.lblGrpType.AutoSize = true;
            this.lblGrpType.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblGrpType.Location = new System.Drawing.Point(12, 86);
            this.lblGrpType.Name = "lblGrpType";
            this.lblGrpType.Size = new System.Drawing.Size(45, 14);
            this.lblGrpType.TabIndex = 7;
            this.lblGrpType.Text = "Quản trị";
            // 
            // FormGroupEditor
            // 
            this.ClientSize = new System.Drawing.Size(525, 160);
            this.Controls.Add(this.chkGrpType);
            this.Controls.Add(this.lblGrpType);
            this.Controls.Add(this.lblGrpName);
            this.Controls.Add(this.lblGrpCode);
            this.Controls.Add(this.txtGrpName);
            this.Controls.Add(this.txtGrpCode);
            this.Controls.Add(this.Toolbars);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormGroupEditor";
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected virtual void OnCancel()
        {
        }

        protected virtual void OnDelete()
        {
        }

        protected virtual void OnEdit()
        {
        }

        protected virtual void OnReset()
        {
            if (this.NewChecked)
            {
                this.LocalCurrentRow["NotDelete"] = false;
                this.chkGrpType.Checked = false;
            }
        }

        public virtual bool OnSave()
        {
            DataRow localCurrentRow;
            if (string.IsNullOrEmpty(this.txtGrpCode.Text))
            {
                MessageBox.Show("Chưa nhập mã nhóm!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (string.IsNullOrEmpty(this.txtGrpName.Text))
            {
                MessageBox.Show("Chưa nhập tên nhóm!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.NewChecked)
            {
                localCurrentRow = this.LocalTable.NewRow();
            }
            else
            {
                localCurrentRow = this.LocalCurrentRow;
            }
            localCurrentRow["GrpCode"] = this.txtGrpCode.Text;
            localCurrentRow["GrpName"] = this.txtGrpName.Text;
            localCurrentRow["GrpType"] = this.chkGrpType.Checked;
            GroupDataAccess access = new GroupDataAccess();
            if (this.ShowMessage(localCurrentRow, access.SaveRow(this.NewChecked, localCurrentRow)))
            {
                return false;
            }
            if (this.NewChecked)
            {
                this.LocalTable.Rows.Add(localCurrentRow);
            }
            this.LocalCurrentRow = localCurrentRow;
            this.LocalTable.AcceptChanges();
            this.txtGrpCode.Focus();
            if (!this.OrderFieldNames.Equals(string.Empty))
            {
                ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
            }
            this.LocalFormLister.SetPosition(this.LocalFieldName, this.LocalCurrentRow[this.LocalFieldName]);
            return true;
        }

        public virtual void OnShow()
        {
            this.txtGrpCode.Text = this.LocalCurrentRow["GrpCode"].ToString();
            this.txtGrpName.Text = this.LocalCurrentRow["GrpName"].ToString();
            this.chkGrpType.Checked = (bool) this.LocalCurrentRow["GrpType"];
            if ((!this.NewChecked && ((bool) this.LocalCurrentRow["NotDelete"])) || (AppInstance.UserType == 1))
            {
                this.chkGrpType.Enabled = false;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F2:
                    if (this.OnSave() && this.btnAdd.Enabled)
                    {
                        this.NewChecked = true;
                        this.OnReset();
                    }
                    return true;

                case Keys.F3:
                    if (this.btnSave.Enabled)
                    {
                        this.OnSave();
                    }
                    return true;

                case (Keys.Control | Keys.Return):
                    if (((base.ActiveControl is IDataGridViewEditingControl) && (base.ActiveControl.Parent != null)) && ((base.ActiveControl.Parent.Parent != null) && (base.ActiveControl.Parent.Parent is DataGridView)))
                    {
                        (base.ActiveControl.Parent.Parent as DataGridView).EndEdit();
                    }
                    if (this.btnSave.Enabled && this.OnSave())
                    {
                        base.Close();
                    }
                    return true;

                case Keys.Escape:
                    if (this.btnClose.Enabled)
                    {
                        base.Close();
                    }
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public bool ShowMessage(DataRow row, int pi_Result)
        {
            switch (pi_Result)
            {
                case -3:
                    row.RejectChanges();
                    MessageBox.Show("Mã lồng nhau!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;

                case -1:
                    row.RejectChanges();
                    MessageBox.Show("Mã đã tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;
            }
            return false;
        }

        [Description("The title text when adding."), Category("Editor properties"), DefaultValue("")]
        public string AddTitleText
        {
            get
            {
                return this.is_AddTitleText;
            }
            set
            {
                this.is_AddTitleText = value;
                this.Text = value;
            }
        }

        [Category("Editor properties"), DefaultValue(""), Description("The title text when editing.")]
        public string EditTitleText
        {
            get
            {
                return this.is_EditTitleText;
            }
            set
            {
                this.is_EditTitleText = value;
            }
        }

        [Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                return this.oCurrentRow;
            }
            set
            {
                this.oCurrentRow = value;
                if ((this.oCurrentRow != null) && (this.oCurrentRow.RowState == DataRowState.Unchanged))
                {
                    this.OnShow();
                }
                this.OnReset();
            }
        }

        [Browsable(false)]
        protected string LocalFieldName
        {
            get
            {
                return this.LocalFormLister.LocalFieldName;
            }
        }

        [Browsable(false)]
        public FormGroupLister LocalFormLister
        {
            get
            {
                return this.oLocalFormLister;
            }
            set
            {
                this.oLocalFormLister = value;
            }
        }

        [Browsable(false)]
        private DataGridView LocalGridBrowser
        {
            get
            {
                return this.LocalFormLister.LocalGridBrowser;
            }
        }

        [Browsable(false)]
        public DataTable LocalTable
        {
            get
            {
                return this.LocalFormLister.LocalTable;
            }
        }

        [Browsable(false)]
        public bool NewChecked
        {
            get
            {
                return this.ib_NewChecked;
            }
            set
            {
                this.ib_NewChecked = value;
                this.Text = this.GetTitleMessage();
            }
        }

        [Browsable(false)]
        private string OrderFieldNames
        {
            get
            {
                return this.LocalFormLister.OrderFieldNames;
            }
        }
    }
}

